home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / cpuinfo.py < prev    next >
Text File  |  2009-11-05  |  6KB  |  193 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import posixpath
  21.  
  22. from checkbox.lib.cache import cache
  23. from checkbox.lib.conversion import string_to_type
  24.  
  25. from checkbox.properties import Path
  26. from checkbox.registries.filename import FilenameRegistry
  27.  
  28.  
  29. def cpuinfo_to_processor(uname, cpuinfo):
  30.     # Default values
  31.     processor = {
  32.         "platform": uname,
  33.         "count": 1,
  34.         "type": uname,
  35.         "model": uname,
  36.         "model_number": "",
  37.         "model_version": "",
  38.         "model_revision": "",
  39.         "cache": 0,
  40.         "bogomips": 0,
  41.         "speed": -1,
  42.         "other": ""}
  43.  
  44.     # Conversion table
  45.     platform_to_conversion = {
  46.         ("i386", "i486", "i586", "i686", "x86_64",): {
  47.             "type": "vendor_id",
  48.             "model": "model name",
  49.             "model_number": "cpu family",
  50.             "model_version": "model",
  51.             "model_revision": "stepping",
  52.             "cache": "cache size",
  53.             "other": "flags",
  54.             "speed": "cpu mhz"},
  55.         ("alpha", "alphaev6",): {
  56.             "count": "cpus detected",
  57.             "type": "cpu",
  58.             "model": "cpu model",
  59.             "model_number": "cpu variation",
  60.             "model_version": ("system type", "system variation",),
  61.             "model_revision": "cpu revision",
  62.             "other": "platform string",
  63.             "speed": "cycle frequency [Hz]"},
  64.         ("ia64",): {
  65.             "type": "vendor",
  66.             "model": "family",
  67.             "model_version": "archrev",
  68.             "model_revision": "revision",
  69.             "other": "features",
  70.             "speed": "cpu mhz"},
  71.         ("ppc64", "ppc",): {
  72.             "type": "platform",
  73.             "model": "cpu",
  74.             "model_version": "revision",
  75.             "vendor": "machine",
  76.             "speed": "clock"},
  77.         ("sparc64", "sparc",): {
  78.             "count": "ncpus probed",
  79.             "type": "type",
  80.             "model": "cpu",
  81.             "model_version": "type",
  82.             "speed": "bogomips"}}
  83.  
  84.     processor["count"] = cpuinfo.get("count", 1)
  85.     processor["bogomips"] = int(round(float(cpuinfo.get("bogomips", "0.0"))))
  86.     for platform, conversion in platform_to_conversion.items():
  87.         if uname in platform:
  88.             for pkey, ckey in conversion.items():
  89.                 if isinstance(ckey, (list, tuple)):
  90.                     processor[pkey] = "/".join([cpuinfo[k] for k in ckey])
  91.                 elif ckey in cpuinfo:
  92.                     processor[pkey] = cpuinfo[ckey]
  93.  
  94.     # Adjust platform and vendor
  95.     if uname[0] == "i" and uname[-2:] == "86":
  96.         processor["platform"] = "i386"
  97.     elif uname[:5] == "alpha":
  98.         processor["platform"] = "alpha"
  99.     elif uname[:5] == "sparc":
  100.         processor["vendor"] = "sun"
  101.  
  102.     # Adjust cache
  103.     if processor["cache"]:
  104.         processor["cache"] = string_to_type(processor["cache"])
  105.  
  106.     # Adjust speed
  107.     try:
  108.         if uname[:5] == "alpha":
  109.             speed = processor["speed"].split()[0]
  110.             processor["speed"] = int(round(float(speed))) / 1000000
  111.         elif uname[:5] == "sparc":
  112.             speed = processor["speed"]
  113.             processor["speed"] = int(round(float(speed))) / 2
  114.         else:
  115.             if uname[:3] == "ppc":
  116.                 # String is appended with "mhz"
  117.                 speed = processor["speed"][:-3]
  118.             else:
  119.                 speed = processor["speed"]
  120.             processor["speed"] = int(round(float(speed)) - 1)
  121.     except ValueError:
  122.         processor["speed"] = -1
  123.  
  124.     # Adjust count
  125.     try:
  126.         processor["count"] = int(processor["count"])
  127.     except ValueError:
  128.         processor["count"] = 1
  129.     else:
  130.         # There is at least one processor
  131.         if processor["count"] == 0:
  132.             processor["count"] = 1
  133.  
  134.     return processor
  135.  
  136.  
  137. class CpuinfoRegistry(FilenameRegistry):
  138.     """Registry for cpuinfo information.
  139.  
  140.     Each item contained in this registry consists of the processor number
  141.     as key and the corresponding processor registry as value.
  142.     """
  143.  
  144.     # Filename where cpuinfo is stored.
  145.     filename = Path(default="/proc/cpuinfo")
  146.  
  147.     # Filename where maximum frequency is stored.
  148.     frequency_filename = Path(default="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq")
  149.  
  150.     def _get_attributes(self):
  151.         count = 0
  152.         attributes = {}
  153.         for block in self.split("\n\n"):
  154.             if not block:
  155.                 continue
  156.  
  157.             count += 1
  158.             if count > 1:
  159.                 continue
  160.  
  161.             for line in block.split("\n"):
  162.                 if not line:
  163.                     continue
  164.  
  165.                 key, value = line.split(":")
  166.                 key, value = key.strip(), value.strip()
  167.  
  168.                 # Handle bogomips on sparc
  169.                 if key.endswith("Bogo"):
  170.                     key = "bogomips"
  171.  
  172.                 attributes[key.lower()] = value
  173.  
  174.         attributes["count"] = count
  175.         return attributes
  176.  
  177.     @cache
  178.     def items(self):
  179.         uname = os.uname()[4].lower()
  180.         attributes = self._get_attributes()
  181.  
  182.         processor = cpuinfo_to_processor(uname, attributes)
  183.  
  184.         # Check for frequency scaling
  185.         if posixpath.exists(self.frequency_filename):
  186.             speed = open(self.frequency_filename).read().strip()
  187.             processor["speed"] = int(speed) / 1000
  188.  
  189.         return processor.items()
  190.  
  191.  
  192. factory = CpuinfoRegistry
  193.